----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files/code/logs/figure_05_phi_domestic_vs_foreign.log
  log type:  text
 opened on:   6 Nov 2022, 10:18:25

. *-------------------------------------------------------------------------------
. 
. 
. 
. *-----------------------------------------------------------------
. clear all

. use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear

. 
. local ctryref FR

. local grvar0 "gdp_pw_ppp" 

. local grvar1 "" 

. local rr ""

. local var3 "A"

. local yy "2016"

. merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta", keepusing(*ctfp* *gdp_pw_ppp*) 
(note: variable year was int, now double to accommodate using data's values)
(note: variable isocode was str2, now str32 to accommodate using data's values)

    Result                           # of obs.
    -----------------------------------------
    not matched                         2,905
        from master                         0  (_merge==1)
        from using                      2,905  (_merge==2)

    matched                               477  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(2,905 observations deleted)

. drop _merge

. rename DA D_A

. 
. foreach var in D_A  {
  2. gen `var'_phi=(-1)*`var'*(0.2)
  3. }
(1 missing value generated)

. *Drop Mexico and Korea due to MNCs data availability
. drop if isocode=="MX" | isocode=="KR" 
(32 observations deleted)

. 
. tempfile temp

. save `temp', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001f.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001f.tmp saved

. 
. 
. *Construct the market shares, Snn and Sfn, from MNC data. Snn=Xnn/Xn 
. *--------------------------------------------------
. 
. 
. use isocode sector1 sector year GO_usd_mnc GO_usd_nt using "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear

. 
. **** CHECK 
. *rename GO_usd_mnc GO_usd_mnc_new
. *rename GO_usd_nt GO_usd_nt_new 
. *merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_0514.dta", keepusing(GO_usd_mnc GO_usd_nt)
. *merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_0830.dta", keepusing(GO_usd_mnc GO_usd_totnat)
. *rename GO_usd_totnat GO_usd_nt
. *merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_0904.dta", keepusing(GO_usd_mnc GO_usd_nt)
. *merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_06152022.dta", keepusing(GO_usd_mnc GO_usd_nt)
. *merge 1:1  isocode sector1 sector year using "${data}/klems_oecd_unido_orbis_sales_emp_exp_06202022.dta", keepusing(GO_usd_mnc GO_usd_nt)
. *br if isocode=="JP" & year==2016
. *order year isocode sector1 sector GO_usd_mnc_new GO_usd_mnc GO_usd_nt_new GO_usd_nt
. **** CHECK 
. 
. drop if sector1==sector
(3,120 observations deleted)

. keep if year==`yy'
(9,000 observations deleted)

. keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isoc
> ode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="
> BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 
(237 observations deleted)

. drop if sector=="Public_Administration (O)"
(27 observations deleted)

. 
. 
. gen Sfn=(GO_usd_mnc)/GO_usd_nt
(87 missing values generated)

. sum Sfn, d

                             Sfn
-------------------------------------------------------------
      Percentiles      Smallest
 1%     .0007499       .0002008
 5%     .0254077       .0002404
10%     .0501889       .0007337       Obs                 399
25%     .1241459       .0007499       Sum of Wgt.         399

50%     .2583931                      Mean           .2876153
                        Largest       Std. Dev.      .2068592
75%     .4005464       .9241048
90%     .5484373       .9254095       Variance       .0427907
95%       .70612       .9257547       Skewness       1.060794
99%     .9241048       1.231964       Kurtosis       4.414278

. replace Sfn=. if Sfn>1 | Sfn<=0
(1 real change made, 1 to missing)

. gen Snn=1-Sfn
(88 missing values generated)

. gsort sector1 sector -Snn

. 
. gen ln_Snn=ln(Snn)
(88 missing values generated)

. gen a=ln_Snn if isocode=="`ctryref'" 
(470 missing values generated)

. by sector1 sector year, sort: egen b=max(a)
(54 missing values generated)

. gen delta_Snn=ln_Snn-b
(96 missing values generated)

. drop a b ln_Snn

. 
. gen ln_Sfn=ln(Sfn)
(88 missing values generated)

. gen a=ln_Sfn if isocode=="`ctryref'" 
(470 missing values generated)

. by sector1 sector year, sort: egen b=max(a)
(54 missing values generated)

. gen delta_Sfn=ln_Sfn-b
(96 missing values generated)

. drop a b ln_Sfn

. 
. tempfile temp0

. save `temp0', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001g.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001g.tmp saved

. 
. 
. *Save Srr (in equation (28) in the paper), this is the market share of domestic firms in France for the reference country (FR) 
. *--------------------------------------------------
. use `temp0', clear

. keep if isocode=="`ctryref'" & year==`yy'
(468 observations deleted)

. drop isocode

. tempfile temp1

. save `temp1', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001h.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001h.tmp saved

. 
. 
. *Bring the sectoral weights (theta_nj)
. *--------------------------------------------------
. use "${data}/sector_weights.dta", clear

. fillin isocode sector year

. keep if year==`yy'
(9,576 observations deleted)

. foreach v in theta theta_fr theta_mso theta_mso_fr {
  2. replace `v'=0 if `v'==.
  3. }
(14 real changes made)
(8 real changes made)
(14 real changes made)
(8 real changes made)

. tempfile weights

. save `weights', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001i.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001i.tmp saved

. 
. 
. 
. *Bring the sectoral weights and Snn, Sfn and Srr to the dataset, then renormalize theta whenever necessary according with the sectoral information availble for each country
. *--------------------------------------------------
. use `temp', clear

. merge m:1 year isocode type sector  using `weights', keepusing(theta*)

    Result                           # of obs.
    -----------------------------------------
    not matched                           353
        from master                         0  (_merge==1)
        from using                        353  (_merge==2)

    matched                               445  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(353 observations deleted)

. drop _merge

. 
. merge m:1 sector1 sector isocode year using `temp0', keepusing(delta_Snn delta_Sfn) 
(note: variable sector was str26, now str37 to accommodate using data's values)

    Result                           # of obs.
    -----------------------------------------
    not matched                            51
        from master                         5  (_merge==1)
        from using                         46  (_merge==2)

    matched                               440  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(46 observations deleted)

. drop _merge

. sum D_A

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
         D_A |        444    1.140697     1.20548  -6.893558   4.946003

. sum year if D_A==.

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
        year |          1        2016           .       2016       2016

. tab isocode sector1 if delta_Snn==. | delta_Sfn==.

                      |                   sector1
              isocode | Manufac..  Market_..  Non-Mar..  Other_G.. |     Total
----------------------+--------------------------------------------+----------
                   AT |         0          0          2          0 |         2 
                   BE |         0          0          3          1 |         4 
                   BG |         0          1          1          0 |         2 
                   CZ |         0          0          2          0 |         2 
                   DE |         0          0          3          0 |         3 
                   DK |         0          1          2          0 |         3 
                   EE |         0          0          1          0 |         1 
                   ES |         0          0          2          0 |         2 
                   FI |         0          0          2          0 |         2 
                   FR |         0          0          2          0 |         2 
                   GB |         0          0          2          0 |         2 
                   GR |         0          0          1          0 |         1 
                   HR |         0          1          1          0 |         2 
                   HU |         0          0          2          0 |         2 
                   IT |         0          1          2          0 |         3 
                   JP |         0          0          1          0 |         1 
                   LT |         1          1          1          0 |         3 
                   LV |         0          0          1          0 |         1 
                   NL |         0          0          2          0 |         2 
                   PL |         0          0          2          0 |         2 
                   PT |         0          0          2          0 |         2 
                   RO |         0          1          3          0 |         4 
                   SE |         0          1          3          0 |         4 
                   SI |         0          0          2          0 |         2 
                   SK |         0          0          3          0 |         3 
----------------------+--------------------------------------------+----------
                Total |         1          7         48          1 |        57 

. replace D_A=. if delta_Snn==. | delta_Sfn==.
(56 real changes made, 56 to missing)

. 
. 
. *Renormalize theta
. *--------------------------------------------------
. foreach j in D_A {
  2. foreach i in theta theta_fr {
  3. display "`i'"
  4. display "`j'"
  5. gen ii=`i' if `j'!=. 
  6. by table isocode year, sort: egen xx=total(ii) 
  7. gen `i'_`j'=ii/xx
  8. replace `i'_`j'=0 if `i'_`j'==.
  9. drop ii xx 
 10. }
 11. }
theta
D_A
(57 missing values generated)
(57 missing values generated)
(57 real changes made)
theta_fr
D_A
(57 missing values generated)
(57 missing values generated)
(57 real changes made)

. *
. tempfile weights_renormalized

. save `weights_renormalized', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001j.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001j.tmp saved

. 
. 
. *Merge all the necessary variables 
. *-----------------------------------
. clear all

. use `temp', clear

. merge m:1 table isocode year sector using  `weights_renormalized', keepusing(theta*D_A)
(note: variable sector was str26, now str37 to accommodate using data's values)

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                               445  (_merge==3)
    -----------------------------------------

. drop _merge

. rename theta_D_A theta

. rename theta_fr_D_A theta_`ctryref'

. 
. merge m:1 sector1 sector isocode year using `temp0', keepusing(delta_Snn delta_Sfn) 

    Result                           # of obs.
    -----------------------------------------
    not matched                            51
        from master                         5  (_merge==1)
        from using                         46  (_merge==2)

    matched                               440  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(46 observations deleted)

. drop _merge

. 
. merge m:1 year sector using `temp1', keepusing(Snn Sfn) 

    Result                           # of obs.
    -----------------------------------------
    not matched                             5
        from master                         5  (_merge==1)
        from using                          0  (_merge==2)

    matched                               440  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(0 observations deleted)

. drop _merge

. rename Snn Snn_`ctryref'

. rename Sfn Sfn_`ctryref'

. 
. gen phi_n_j=(-1)*D_A*(0.2)
(1 missing value generated)

. gen phi_nn_j=((delta_Snn)*(0.2))+phi_n_j
(57 missing values generated)

. gen phi_fn_j=((delta_Sfn)*(0.2))+phi_n_j
(57 missing values generated)

. 
. gen phi_n_j_theta=phi_n_j*theta
(1 missing value generated)

. gen phi_nn_j_theta=phi_nn_j*theta*(Snn_`ctryref')
(57 missing values generated)

. gen phi_fn_j_theta=phi_fn_j*theta*(Sfn_`ctryref')
(57 missing values generated)

. 
. gen phi_n_j_theta_`ctryref'=phi_n_j*theta_`ctryref'
(1 missing value generated)

. gen phi_nn_j_theta_`ctryref'=phi_nn_j*theta_`ctryref'*(Snn_`ctryref')
(57 missing values generated)

. gen phi_fn_j_theta_`ctryref'=phi_fn_j*theta_`ctryref'*(Sfn_`ctryref')
(57 missing values generated)

. 
. collapse (sum) phi_nn_agg=phi_nn_j_theta phi_fn_agg=phi_fn_j_theta  phi_n_agg=phi_n_j_theta phi_nn_agg_`ctryref'=phi_nn_j_theta_`ctryref' phi_fn_agg_`ctryref'=phi_fn_j_theta_`c
> tryref' phi_n_agg_`ctryref'=phi_n_j_theta_`ctryref' (mean) Snn* Sfn* ln_`grvar0', by(isocode year)

. 
.                                 
. tempfile figure_foreign_domestic

. save `figure_foreign_domestic', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001k.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_27c8_00001k.tmp saved

. 
. 
. 
. *---------------------------------------------------------
. display "Figure 5: Aggregate firm-embedded productivity: domestic vs foreign firms"
Figure 5: Aggregate firm-embedded productivity: domestic vs foreign firms

. *---------------------------------------------------------
. use `figure_foreign_domestic', clear

. 
. global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(maroon)  msize(medium) mlabsize(vsmall)

. global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(green) msize(medium) mlabsize(vsmall)

. global opt3 msymbol(none) mlabcolor(none) 

. 
. gen phi_nn_agg_w=phi_nn_agg

. gen phi_fn_agg_w=phi_fn_agg

. gen phi_n_agg_sq=phi_nn_agg_w+phi_fn_agg_w

. 
. local LHS1 phi_nn_agg_w

. local LHS2 phi_fn_agg_w

. local RHS phi_n_agg_sq

. sum `LHS1' `LHS2' `RHS' 

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
phi_nn_agg_w |         25   -.2152718    .1399537  -.4503294   .0313026
phi_fn_agg_w |         25   -.0359032    .0332643  -.0997929   .0202564
phi_n_agg_sq |         25   -.2511749    .1586422  -.5232652   .0296915

. local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:nn}, {&Delta}`=ustrunescape("\u03D5\u0303")'{sub:Fn}" 

. local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"

. local firm_emb_xnn "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:nn}" 

. local firm_emb_xfn "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:Fn}" 

. 
. 
. reg `LHS1' `RHS' 

      Source |       SS           df       MS      Number of obs   =        25
-------------+----------------------------------   F(1, 23)        =    657.16
       Model |  .454192386         1  .454192386   Prob > F        =    0.0000
    Residual |  .015896398        23  .000691148   R-squared       =    0.9662
-------------+----------------------------------   Adj R-squared   =    0.9647
       Total |  .470088784        24  .019587033   Root MSE        =    .02629

------------------------------------------------------------------------------
phi_nn_agg_w |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
phi_n_agg_sq |   .8671527   .0338268    25.64   0.000     .7971766    .9371288
       _cons |   .0025352   .0099918     0.25   0.802    -.0181343    .0232048
------------------------------------------------------------------------------

. mat b = e(b)

. mat V = e(V)

. global c1: display %-03.2fc round(b[1,1],0.01)

. global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

. 
. reg `LHS2' `RHS' 

      Source |       SS           df       MS      Number of obs   =        25
-------------+----------------------------------   F(1, 23)        =     15.42
       Model |   .01065993         1   .01065993   Prob > F        =    0.0007
    Residual |  .015896401        23  .000691148   R-squared       =    0.4014
-------------+----------------------------------   Adj R-squared   =    0.3754
       Total |  .026556332        24  .001106514   Root MSE        =    .02629

------------------------------------------------------------------------------
phi_fn_agg_w |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
phi_n_agg_sq |   .1328473   .0338268     3.93   0.001     .0628712    .2028234
       _cons |  -.0025352   .0099918    -0.25   0.802    -.0232048    .0181343
------------------------------------------------------------------------------

. mat b = e(b)

. mat V = e(V)

. global c2: display %-03.2fc round(b[1,1],0.01)

. global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

. 
. global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(maroon)  msize(medium) mlabsize(vsmall)

. global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(green) msize(medium) mlabsize(vsmall)

. global opt3 msymbol(none) mlabcolor(none) 

. two (scatter `LHS1' `RHS', sort $opt1 mcolor(maroon)) (lfit `LHS1' `RHS', lcolor(maroon)) (line `LHS1' `RHS', lcolor(none))  ///
> (scatter `LHS2' `RHS', sort $opt2 mcolor(green)) (lfit `LHS2' `RHS', lcolor(green)) (line `LHS2' `RHS', lcolor(none)), ylabel(-0.6(0.2)0.2, format(%5.1f) labsize(medlarge)) xla
> bel(-0.6(0.2)0, format(%5.1f) labsize(medlarge))  xscale(titlegap(*1)) ///
> ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
> legend(order(2 "`firm_emb_xnn':  $c1 ($se1)" 5 "`firm_emb_xfn':  $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

. graph export "${rmain}/fig_05.pdf", replace
(file F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files/results/main/fig_05.pdf written in PDF format)

. 
. 
. 
. 
. 
. 
end of do-file

. clear all

. 
. use "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear

. global typeden=1

. 
. include "set_directories.do"

. /***************************************************************************************
> Firm-embedded productivity and cross-country income differences
> Alviarez, Cravino and Ramondo
> Journal of Political Economy (2022)
> 
> Program: set_directories.do
> Date: October 2022
> 
> Description: Sets directory paths
> *****************************************************************************************/
. 
. *Set root directory here
. global root "F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files" 

. 
. 
. *All other directories set automatically
. 
. *Code
. global code "${root}/code"

. global clogs "${root}/code/logs"

. global cmain "${root}/code/main"

. global cappendix "${root}/code/appendix"

. 
. *Data
. global data "${root}/data/analysis"

. global tmp  "${root}/data/tmp"

. 
. *Results
. global rmain "${root}/results/main"

. global rappendix  "${root}/results/appendix"

. 
. if $typeden==0 {
. local denominator "_exp_ko"
. }

. if $typeden==1 {
. local denominator "_ko"
. }

. *
. 
. 
. 
. /*******************************************************************************
> *Stata packages to install:
>    1) gtools
>    2) reghdfe
> *******************************************************************************/
. 
. 
. ************** ESTO DE ACA ES LO QUE TENGO QUE BORRAR **************
. *global maindirectory "F:/Dropbox (UBC-Umich)"
. *global maindirectory "C:\Users\cheoy\Dropbox (UBC-Umich)"
. *global tfp "${maindirectory}/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical" 
. *global tfp_bef "${maindirectory}/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data" 
. *global orbis_historical "${maindirectory}/Master_Dataset/Orbis_master/Javier_Florian/orbis_data/final_data"
. ************** ESTO DE ACA ES LO QUE TENGO QUE BORRAR **************
. 
. *global inputs "${tfp}/data/inputs"
. *global code "${tfp}/code"
. *global output "${tfp}/data/output"
. *global results "${tfp}/results"
. 
. 
. 
. 
. 
. 
. 
. . 
. set memory 64g
set memory ignored.
    Memory no longer needs to be set in modern Statas; memory adjustments are performed on the fly automatically.

. 
. global lf "LF"

. 
. 
. 
. *Log

. 
. cap log close
